package com.may.wlrj.controller;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.async.DeferredResult;

import com.may.wlrj.entity.UserEntity;
import com.may.wlrj.services.UserService;
import com.may.wlrj.util.XinLongUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
	private Logger log = Logger.getLogger(UserController.class);
	private UserService userService;
    @Autowired
    public void setUserService(UserService userService){
        this.userService = userService;
    }

    /**
     * 用户添加
     * @param user
     * @param model
     * @param session
     * @return
     * @throws Exception 
     */
    @RequestMapping(value = "/useradd",method = RequestMethod.POST,produces="application/json;charset=UTF-8;")
    public @ResponseBody DeferredResult<String> Adduser(@RequestBody String objnode,HttpServletRequest request, HttpSession session) throws Exception{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();
        userService._insertuser(deferredResult,objnode,request, session);
        return deferredResult;
    }
    
    @RequestMapping(value = "/useredit",method = RequestMethod.POST,produces="application/json;charset=UTF-8;")
    public @ResponseBody DeferredResult<String> Edituser(@RequestBody String objnode, HttpServletRequest request, HttpSession session) throws Exception{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();
        userService.C_updateuser(deferredResult,objnode,request, session);
        return deferredResult;
    }
    
    @RequestMapping(value = "/userdel",method = RequestMethod.POST,produces="application/json;charset=UTF-8;")
    public @ResponseBody DeferredResult<String> Deluser(@RequestBody String objnode,HttpServletRequest request ,HttpSession session) throws JsonGenerationException, JsonMappingException, IOException{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();
        userService.C_UserDelete(deferredResult,objnode,request ,session);
        return deferredResult;
    }
    
    @RequestMapping(value = "/userpsw",method = RequestMethod.POST,produces="text/html;charset=UTF-8;")
    public @ResponseBody String Pswuser(@RequestBody String objnode, HttpServletRequest request,HttpSession session) throws Exception{
        //获取用户名和密码
    	XinLongUtil.xinLongUtil.printlog(log,"---------------------->>>>userpsw:");    	
        return userService.updatepsw(objnode,request, session);
    }
    
    @RequestMapping(value="/getInitCore",method=RequestMethod.GET,produces="text/html;charset=UTF-8;") 
	public @ResponseBody String getInitCore(HttpServletRequest request ,HttpServletResponse response, HttpSession session) throws IOException{	
		return userService.getUserinitCore(session);
		
	}

    @RequestMapping(value="/getUserlist",method=RequestMethod.POST,produces="text/html;charset=UTF-8;") 
	public @ResponseBody String getUserList(@RequestBody String objnode,HttpServletRequest request, HttpSession session) throws IOException{
    	XinLongUtil.xinLongUtil.printlog(log,"---------------------->>>>getUserlist");
    	return userService.getUserlist(objnode,request,session);

		
	}
    
    @RequestMapping(value="/getUserlistbyauth",method=RequestMethod.GET,produces="text/html;charset=UTF-8;") 
	public @ResponseBody String getUserList(HttpSession session) throws IOException{
    	XinLongUtil.xinLongUtil.printlog(log,"---------------------->>>>getUserlistbyauth");
    	return userService.getUserlistbyauth(session);

		
	}
    
    @RequestMapping(value="/user_reset",method=RequestMethod.POST,produces="text/html;charset=UTF-8;") 
	public @ResponseBody DeferredResult<String> user_Reset(@RequestBody String objnode, HttpServletRequest request, HttpSession session) throws IOException{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();	    	
		userService.C_resetUserPsw(deferredResult,objnode,request, session);	
		return deferredResult;
	}
    
    @RequestMapping(value="/user_unlock",method=RequestMethod.POST,produces="text/html;charset=UTF-8;") 
   	public @ResponseBody DeferredResult<String> user_Unlock(@RequestBody String objnode, HttpServletRequest request, HttpSession session) throws IOException{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();    	
   		userService.C_unlockUser(deferredResult,objnode,request, session);	
   		return deferredResult;
   	}

    @RequestMapping(value="/user_lock",method=RequestMethod.POST,produces="text/html;charset=UTF-8;") 
   	public @ResponseBody DeferredResult<String> user_Lock(@RequestBody String objnode, HttpServletRequest request, HttpSession session) throws IOException{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();	    	
   		userService.C_lockUser(deferredResult,objnode,request, session);		
   		return deferredResult;
   	}
    
    @RequestMapping(value="/userupdaterole",method=RequestMethod.POST,produces="text/html;charset=UTF-8;") 
   	public @ResponseBody DeferredResult<String> userupdaterole(@RequestBody String objnode, HttpServletRequest request, HttpSession session) throws IOException{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();  	
   		userService.C_updateUserRole(deferredResult,objnode,request, session);	
   		return deferredResult;
   	}

    @RequestMapping(value="/getUserNamelist",method=RequestMethod.POST,produces="text/html;charset=UTF-8;") 
   	public @ResponseBody DeferredResult<String> getUserNamelist(@RequestBody String objnode, HttpServletRequest request, HttpSession session) throws IOException{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();  	
   		userService.getUserNamelist(deferredResult,objnode,request, session);	
   		return deferredResult;
   	}    
    
    @RequestMapping(value="/getUserNamelist2",method=RequestMethod.POST,produces="text/html;charset=UTF-8;") 
   	public @ResponseBody DeferredResult<String> getUserNamelist2(@RequestBody String objnode,HttpServletRequest request, HttpSession session) throws IOException{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();  	
   		userService.getUserNamelist2(deferredResult,objnode,request, session);	
   		return deferredResult;
   	}  
    
    @RequestMapping(value="/getUserNamelist3",method=RequestMethod.GET,produces="text/html;charset=UTF-8;") 
   	public @ResponseBody DeferredResult<String> getUserNamelist3(HttpServletRequest request, HttpSession session) throws IOException{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();  	
   		userService.getUserNamelist3(deferredResult,request, session);	
   		return deferredResult;
   	} 
    
    @RequestMapping(value = "/fingeradd",method = RequestMethod.POST,produces="application/json;charset=UTF-8;")
    public @ResponseBody DeferredResult<String> Addfinger(@RequestBody String objnode,HttpServletRequest request, HttpSession session) throws Exception{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();
        userService._insertfinger(deferredResult,objnode,request, session);
        return deferredResult;
    }
    
    @RequestMapping(value = "/fingerauth",method = RequestMethod.POST,produces="application/json;charset=UTF-8;")
    public @ResponseBody DeferredResult<String> Authfinger(@RequestBody String objnode,HttpServletRequest request, HttpSession session) throws Exception{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();
        userService.authfinger(deferredResult,objnode,request, session);
        return deferredResult;
    }
    
    @RequestMapping(value = "/fingerquery",method = RequestMethod.POST,produces="application/json;charset=UTF-8;")
    public @ResponseBody DeferredResult<String> Queryfinger(@RequestBody String objnode,HttpServletRequest request, HttpSession session) throws Exception{
    	DeferredResult<String> deferredResult = new DeferredResult<String>();
        userService.queryfinger(deferredResult,objnode,request, session);
        return deferredResult;
    }
}